const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    dfk: '',
    dfh: '',
    dsh: '',
    info: '',
    telBlockState: false,
    getCodeText: "获取验证码",
    tel: "",
    telCode: "",
    applyMsgState: true,
    getCodeState: false,
  },
  // 联系我们
  call() {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: '' + that.data.info.phone,
    })
  },
  // 去绑定
  bind() {
    this.setData({
      telBlockState: true
    })
  },
  telIpt(e) { //电话号码
    this.setData({
      tel: e.detail.value
    })
  },
  codeIpt(e) { //验证码
    this.setData({
      telCode: e.detail.value
    })
  },
  verification() { //验证手机
    let that = this;
    var tel = that.data.tel,
      telReg = /^1\d{10}$/;
    if (tel === "" || !tel) {
      app.showToast("电话号码不能为空！")
      return false;
    } else if (!telReg.test(tel)) {
      app.showToast("电话号码格式错误！")
      return false;
    } else {
      return true;
    }
  },
  telConfirm() { // 确认
    let that = this;
    if (that.verification()) {
      if (that.data.telCode == "" || !that.data.telCode) {
        app.showToast("请输入验证码！")
      } else {
        wx.request({
          url: app.buildUrl('/shop/shop/save-shop-info'),
          header: app.buildHeader(),
          method: 'POST',
          data: {
            phone: that.data.tel,
            code: that.data.telCode
          },
          success: function(res) {
            if (res.data.code == 200) {
              app.showToast(res.data.msg)
              setTimeout(() => {
                that.setData({
                  tel: "",
                  telCode:"",
                  telBlockState: false
                })
                that.onShow()
              }, 1000)
            } else {
              app.showToast(res.data.msg)
            }
          },
          fail: function(res) {
            console.log(res)
          }
        })
      }
    }
  },
  getCodeFun() { // 获取验证码
    var that = this;
    if (that.verification()) {
      let [times, state, timer] = [90, that.data.getCodeState, null];
      if (state) {
        return
      }
      wx.request({
        url: app.buildUrl('/shop/shop/send-code'),
        header: app.buildHeader(),
        method: 'POST',
        data: {
          phone: that.data.tel
        },
        success: function(res) {
          if (res.data.code == 200) {
            that.setData({
              getCodeState: true,
              getCodeText: times + "s"
            })
            timer = setInterval(function() {
              times--;
              that.setData({
                getCodeText: times + "s"
              })
              if (times == 0) {
                clearTimeout(timer)
                timer = null;
                times = 180;
                that.setData({
                  getCodeText: "获取验证码",
                  getCodeState: false
                })
              }
            }, 1000)
            app.showToast(res.data.msg)
          } else {
            app.showToast(res.data.msg)
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    }
  },
  telClose() {
    this.setData({
      telBlockState: false
    })
  },
  apply() {
    this.setData({
      applyMsgState: false
    })
  },
  copyUrl(e) {
    wx.setClipboardData({
      data: e.target.dataset.url,
      success: function(res) {
        app.showToast('复制成功')
      }
    })
  },
  back() {
    this.setData({
      applyMsgState: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.request({
      url: app.buildUrl('/shop/shop/get-user'),
      header: app.buildHeader(),
      method: 'GET',
      success: res => {
        this.setData({
          dfk: 0,
          dfh: 0,
          dsh: 0,
          info: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})